<template>
  <div class="admin-dashboard">
    <!-- 管理员侧边栏 -->
    <nav class="admin-sidebar">
      <div class="admin-logo">
        <img src="/logo.png" alt="乐享音乐" class="logo-image" />
        <span>乐享管理台</span>
      </div>

      <div class="admin-nav">
        <router-link to="/admin/dashboard" class="nav-item" active-class="active">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
            <path d="M3 13H11V3H3V13ZM3 21H11V15H3V21ZM13 21H21V11H13V21ZM13 3V9H21V3H13Z"/>
          </svg>
          <span>仪表板</span>
        </router-link>

        <router-link to="/admin/users" class="nav-item" active-class="active">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
            <path d="M16 4C18.2 4 20 5.8 20 8S18.2 12 16 12 12 10.2 12 8 13.8 4 16 4M16 14C20.4 14 24 15.8 24 18V20H8V18C8 15.8 11.6 14 16 14M8.5 4C10.4 4 12 5.6 12 7.5S10.4 11 8.5 11 5 9.4 5 7.5 6.6 4 8.5 4M8.5 13C11.5 13 17 14.5 17 17.5V20H0V17.5C0 14.5 5.5 13 8.5 13Z"/>
          </svg>
          <span>用户管理</span>
        </router-link>

        <router-link to="/admin/music" class="nav-item" active-class="active">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
            <path d="M12 3V13.55C11.41 13.21 10.73 13 10 13C7.79 13 6 14.79 6 17S7.79 21 10 21 14 19.21 14 17V7H18V3H12Z"/>
          </svg>
          <span>音乐管理</span>
        </router-link>

        <router-link to="/admin/categories" class="nav-item" active-class="active">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
            <path d="M4,6H2V20A2,2 0 0,0 4,22H18V20H4V6M20,2H8A2,2 0 0,0 6,4V16A2,2 0 0,0 8,18H20A2,2 0 0,0 22,16V4A2,2 0 0,0 20,2M20,16H8V4H20V16M16,6V14L13,11L10,14V6H16Z"/>
          </svg>
          <span>分类管理</span>
        </router-link>
      </div>

      <div class="admin-user">
        <div class="admin-avatar">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
            <path d="M12,4A4,4 0 0,1 16,8A4,4 0 0,1 12,12A4,4 0 0,1 8,8A4,4 0 0,1 12,4M12,14C16.42,14 20,15.79 20,18V20H4V18C4,15.79 7.58,14 12,14Z"/>
          </svg>
        </div>
        <div class="admin-info">
          <span class="admin-name">管理员</span>
          <span class="admin-role">超级管理员</span>
        </div>
        <button @click="logout" class="logout-btn">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
            <path d="M16,17V14H9V10H16V7L21,12L16,17M14,2A2,2 0 0,1 16,4V6H14V4H5V20H14V18H16V20A2,2 0 0,1 14,22H5A2,2 0 0,1 3,20V4A2,2 0 0,1 5,2H14Z"/>
          </svg>
        </button>
      </div>
    </nav>

    <!-- 主内容区域 -->
    <main class="admin-content">
      <CustomScrollbar>
        <slot></slot>
      </CustomScrollbar>
    </main>

    <!-- 确认对话框 -->
    <div v-if="confirmDialog.visible" class="confirm-overlay" @click="handleCancel">
      <div class="confirm-dialog" @click.stop>
        <div class="confirm-icon">
          <svg v-if="confirmDialog.type === 'danger'" width="48" height="48" viewBox="0 0 24 24" fill="#ff4757">
            <path d="M12,2C17.53,2 22,6.47 22,12C22,17.53 17.53,22 12,22C6.47,22 2,17.53 2,12C2,6.47 6.47,2 12,2M15.59,7L12,10.59L8.41,7L7,8.41L10.59,12L7,15.59L8.41,17L12,13.41L15.59,17L17,15.59L13.41,12L17,8.41L15.59,7Z"/>
          </svg>
          <svg v-else-if="confirmDialog.type === 'warning'" width="48" height="48" viewBox="0 0 24 24" fill="#ffa502">
            <path d="M13,14H11V10H13M13,18H11V16H13M1,21H23L12,2L1,21Z"/>
          </svg>
          <svg v-else width="48" height="48" viewBox="0 0 24 24" fill="#1DB954">
            <path d="M11,9H13V7H11M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M11,17H13V11H11V17Z"/>
          </svg>
        </div>
        <div class="confirm-header">
          <h3>{{ confirmDialog.title }}</h3>
        </div>
        <div class="confirm-body">
          <p>{{ confirmDialog.message }}</p>
          <p v-if="confirmDialog.detail" class="confirm-detail">{{ confirmDialog.detail }}</p>
        </div>
        <div class="confirm-footer">
          <button @click="handleCancel" class="confirm-btn cancel">
            {{ confirmDialog.cancelText }}
          </button>
          <button @click="handleConfirm" class="confirm-btn confirm" :class="confirmDialog.type">
            {{ confirmDialog.confirmText }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CustomScrollbar from '@/components/CustomScrollbar.vue'

export default {
  name: 'AdminLayout',
  components: {
    CustomScrollbar
  },
  emits: ['show-category-modal'],
  data() {
    return {
      confirmDialog: {
        visible: false,
        title: '',
        message: '',
        detail: '',
        type: 'info', // 'info', 'warning', 'danger'
        confirmText: '确定',
        cancelText: '取消',
        onConfirm: null,
        onCancel: null
      }
    }
  },
  mounted() {
    // 提供全局确认对话框方法
    this.$root.showConfirm = this.showConfirm
  },
  methods: {
    showConfirm(options) {
      return new Promise((resolve, reject) => {
        this.confirmDialog = {
          visible: true,
          title: options.title || '确认操作',
          message: options.message || '确定要执行此操作吗？',
          detail: options.detail || '',
          type: options.type || 'warning',
          confirmText: options.confirmText || '确定',
          cancelText: options.cancelText || '取消',
          onConfirm: () => {
            this.confirmDialog.visible = false
            resolve(true)
          },
          onCancel: () => {
            this.confirmDialog.visible = false
            resolve(false)
          }
        }
      })
    },

    handleConfirm() {
      if (this.confirmDialog.onConfirm) {
        this.confirmDialog.onConfirm()
      }
    },

    handleCancel() {
      if (this.confirmDialog.onCancel) {
        this.confirmDialog.onCancel()
      }
    },

    async logout() {
      // 使用自定义确认对话框
      const confirmed = await this.showConfirm({
        title: '退出登录',
        message: '确定要退出管理员账户吗？',
        detail: '退出后需要重新登录才能访问管理后台',
        type: 'warning',
        confirmText: '退出',
        cancelText: '取消'
      })

      if (confirmed) {
        // 清除管理员认证信息
        localStorage.removeItem('adminToken')
        localStorage.removeItem('adminTokenExpiry')
        localStorage.removeItem('adminInfo')
        
        // 清理旧的key以防冲突
        localStorage.removeItem('token')
        localStorage.removeItem('userRole')
        localStorage.removeItem('tokenExpiry')
        
        // 注意：不清理前台用户数据，管理员退出不影响前台用户登录
        
        // 显示退出成功消息
        if (window.app && window.app.showToast) {
          window.app.showToast('管理员已退出登录', 'success')
        }
        
        // 跳转到管理员登录页
        this.$router.push('/admin/login').catch(err => {
          console.error('跳转到管理员登录页失败:', err)
          // 备选方案：直接跳转
          window.location.href = '/admin/login'
        })
      }
    }
  }
}
</script>

<style scoped>
/* 管理员布局样式 */
.admin-dashboard {
  display: flex;
  height: 100vh;
  background: #121212;
  color: white;
}

/* 侧边栏样式 */
.admin-sidebar {
  width: 280px;
  background: #1a1a1a;
  border-right: 1px solid #282828;
  display: flex;
  flex-direction: column;
  padding: 24px;
  position: fixed;
  height: 100vh;
  left: 0;
  top: 0;
  z-index: 100;
}

.admin-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid #282828;
}

.logo-image {
  width: 32px;
  height: 32px;
  object-fit: contain;
  border-radius: 6px;
}

.admin-logo span {
  font-size: 20px;
  font-weight: 700;
  color: white;
}

.admin-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 8px;
  color: #b3b3b3;
  text-decoration: none;
  transition: all 0.3s ease;
  cursor: pointer;
  user-select: none;
}

.nav-item:hover {
  background: #282828;
  color: white;
}

.nav-item.active {
  background: #1db954;
  color: white;
}

.nav-item svg {
  flex-shrink: 0;
}

.admin-user {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  background: #282828;
  border-radius: 12px;
  margin-top: 24px;
}

.admin-avatar {
  width: 40px;
  height: 40px;
  background: #404040;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #b3b3b3;
}

.admin-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.admin-name {
  font-weight: 600;
  color: white;
  font-size: 14px;
}

.admin-role {
  font-size: 12px;
  color: #b3b3b3;
}

.logout-btn {
  background: transparent;
  border: none;
  color: #b3b3b3;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: #404040;
  color: white;
}

/* 主内容区域 */
.admin-content {
  flex: 1;
  margin-left: 280px;
  background: #121212;
  overflow: hidden;
}

/* 确认对话框样式 */
.confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  animation: fadeIn 0.3s ease;
}

.confirm-dialog {
  background: #1a1a1a;
  border-radius: 16px;
  padding: 0;
  width: 90%;
  max-width: 420px;
  overflow: hidden;
  animation: slideIn 0.3s ease;
  border: 1px solid #333;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.confirm-icon {
  text-align: center;
  padding: 32px 32px 16px;
}

.confirm-header {
  text-align: center;
  padding: 0 32px 16px;
}

.confirm-header h3 {
  margin: 0;
  color: white;
  font-size: 20px;
  font-weight: 600;
}

.confirm-body {
  padding: 16px 32px;
  text-align: center;
}

.confirm-body p {
  margin: 0 0 8px 0;
  color: #b3b3b3;
  font-size: 16px;
  line-height: 1.5;
}

.confirm-detail {
  color: #999 !important;
  font-size: 14px !important;
  margin-top: 8px !important;
}

.confirm-footer {
  padding: 16px 32px;
  display: flex;
  gap: 12px;
  justify-content: center;
}

.confirm-btn {
  padding: 12px 24px;
  border: none;
  border-radius: 25px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 100px;
}

.confirm-btn.cancel {
  background: transparent;
  color: #b3b3b3;
  border: 1px solid #535353;
}

.confirm-btn.cancel:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border-color: #727272;
}

.confirm-btn.confirm {
  background: #1db954;
  color: white;
}

.confirm-btn.confirm:hover {
  background: #1ed760;
  transform: scale(1.02);
}

.confirm-btn.confirm.danger {
  background: #ff4757;
}

.confirm-btn.confirm.danger:hover {
  background: #ff3838;
}

.confirm-btn.confirm.warning {
  background: #ffa502;
  color: #000;
}

.confirm-btn.confirm.warning:hover {
  background: #ff3838;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideIn {
  from { 
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to { 
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-sidebar {
    width: 100%;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }
  
  .admin-sidebar.mobile-open {
    transform: translateX(0);
  }
  
  .admin-content {
    margin-left: 0;
  }
}
</style> 